/**
This file is part of Gel2DE.

    Gel2DE is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gel2DE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Gel2DE.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (C) 2012, University of Bergen
*/

#ifndef gel2de_StringUtils_h
#define gel2de_StringUtils_h

#include <string>
#include <vector>
#include <sstream>
#include <limits>
#include "tinyxml.h"

namespace gel2de {
 
   template <class T>
   inline std::string toString(const T& t) {
      std::ostringstream oss;
      oss << t;
      return oss.str();
   }

   inline std::string toString(const std::string& t) {
      return t;
   }

   inline std::string toString(const char* t) {
      return t ? t : "";
   }


   template <class S, class T>
   inline void fromString(const S& s, T& t) {
      std::istringstream iss(toString(s));
      iss >> t;
   }

   /// Converts the value of a string to a specified type.
   template <class T>
   inline T fromString(const std::string& s) {
      T t;
      std::istringstream(s) >> t;
      return t;
   }

   inline void fromString(const std::string& s, std::string& t) {
      t = s;
   }

   inline void fromString(const char* s, std::string& t) {
      t = s ? s : "";
   }


   //--------------------------------------------------------------------------

   template <class T>
   inline std::string xmlElement(const std::string& name, const T& value) {
      std::ostringstream oss;
      oss << "<" << name << ">" << value << "</" << name << ">";
      return oss.str();
   }

   template <class T>
   inline std::string xmlAttribute(const std::string& name, const T& value) {
      std::ostringstream oss;
      oss << " " <<  name << "='" << value << "'";
      return oss.str();
   }
   
   //--------------------------------------------------------------------------

   /// Returns true if the string s starts with the specified prefix.
   bool startsWith(const std::string& s, const std::string& prefix);

   /// Returns true if the string s ends with the specified suffix.
   bool endsWith(const std::string& s, const std::string& suffix);

   /// Returns a string with all characters converted to lower case.
   std::string toLowerCase(const std::string& s);

   /// Returns a string with all characters converted to upper case.
   std::string toUpperCase(const std::string& s);

   /// Returns true if and only if the string equals "TRUE" ignoring case.
   bool stringToBool(const std::string& s);

   /// Returns "true" or "false".
   inline std::string boolToString(bool b) {
      return b ? "true" : "false";
   }

   /// Returns a string with leading and trailing whitespaces removed.
   std::string trim(const std::string& s);

   /// Returs a vector of the substrings in s separated by separator.
   std::vector<std::string> split(const std::string& s,
                                  const std::string& separator,
                                  int limit = std::numeric_limits<int>::max());

} // gel2de::

#endif // gel2de_StringUtils_h
